import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        ArrayList<user> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("----欢迎进入管理系统-----------");
            System.out.println("1：登录，2：注册，3:忘记密码，任意键退出");
            String num = sc.next();
            switch (num) {
                case "1" -> log(list);//登录
                case "2" -> creat(list);
                case "3" -> forgetpassword(list);//忘记
                default -> {
                    System.out.println("谢谢");
                    System.exit(0);
                }
            }
        }
    }//界面
    public static void creat(ArrayList<user> list) {//注册

        Scanner sc = new Scanner(System.in);
        String username;
        String password;
        String personid;
        String phonenumber;
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();
            boolean flag = check(username);
            if (!flag) {//格式不正确
                System.out.println("格式错误，重新输入");
                continue;
            }

            boolean flag2 = contains(list, username);
            if (flag2) {//唯一
                System.out.println("成功");
                break;
            } else {
                System.out.println("已存在，重新输入");
            }
        }
        while (true) {
            System.out.println("请输入密码");
            password = sc.next();
            System.out.println("请再次输入密码");
            String password2 = sc.next();
            if (!password.equals(password2)) {
                System.out.println("密码不一致请重新输入");
                continue;
            } else {
                System.out.println("成功");
                break;
            }
        }
        //长度是18位 不能以零开头 前17位必须是数字 最后一位可以是Xx;
        while (true) {
            System.out.println("请输入身份证号码");
            personid = sc.next();
            boolean flag3 = checkper(personid);
            if (flag3) {
                System.out.println("成功");
                break;
            } else {
                System.out.println("请重新输入");
                continue;
            }
        }
        //长度11位 不能一零开头 必须是数字
        while (true) {
            System.out.println("请输入验证码");
            phonenumber = sc.next();
            boolean flag4 = checkphone(phonenumber);
            if (flag4) {
                System.out.println("成功");
                break;
            } else {
                System.out.println("请重新输入");
                continue;
            }
        }
        user u = new user(username, password, personid, phonenumber);
        list.add(u);
        System.out.println("注册完成");
        System.out.println("用户名       密码        身份证        验证码");
        print(list);
    }//注册
    public static ArrayList<user> print(ArrayList<user> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getUsername() + "  " + list.get(i).getPassword() + " " + list.get(i).getPersonid() + " " + list.get(i).getPhonenumber());
        }
        return list;
    }//遍历结果
    public static boolean checkphone(String phonenumber) {
        if (phonenumber.length() != 4) {
            return false;
        }
        if (phonenumber.charAt(0) == 0) {
            return false;
        }
        for (int i = 0; i < phonenumber.length(); i++) {
            if (!(phonenumber.charAt(i) > '0' && phonenumber.charAt(i) <= '9')) {
                return false;
            }
        }
        return true;
    }//满足验证码
    public static boolean checkper(String personnum) {
        if (personnum.length() != 3) {
            return false;
        }
        if (personnum.charAt(0) == '0') {
            return false;
        }
        for (int i = 0; i < 3; i++) {
            if (!(personnum.charAt(i) > '0' && personnum.charAt(i) <= '9')) {
                return false;
            }
        }
//        char endnum=personnum.charAt(personnum.length()-1);
//        if((endnum>='0'&&endnum<='9')||(endnum=='x')||(endnum=='X'))
//        {
//            return true;
//        }
//        else{
//            return false;
//        }
        return true;
    }//满足身份证要求
    public static boolean check(String username) {//判断是否格式正确
        int cout = 0;
        if ((username.length() < 3) || (username.length() > 15)) {
            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char chs = username.charAt(i);
            if (!((chs > 'a' && chs < 'z') || (chs > 'A' && chs < 'Z') || (chs > '0' && chs < '9'))) {
                return false;
            }
        }
        for (int i = 0; i < username.length(); i++) {
            char chs = username.charAt(i);
            if ((chs > 'a' && chs < 'z') || chs < 'Z' && chs > 'A') {
                cout++;
                break;
            }
        }
        return cout > 0;
    }//满足用户名要求
    public static boolean contains(ArrayList<user> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(username)) {
                return false;
            }
        }
        return true;
    }//唯一性判断
    public static void log(ArrayList<user> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        System.out.println("输入用户名");
        username = sc.next();
        boolean flag5 = contains(list, username);
        if (flag5) {
            System.out.println("未注册,请先注册");
            return;
        }

            System.out.println("输入密码");
            String password = sc.next();
            //封装的思想  把一些零三的数据封装成一个对象
        while (true) {
            String rigthcode = Sysnumber();
            System.out.println("输入验证码" + rigthcode);
            String yznum = sc.next();
            if (yznum.equalsIgnoreCase(rigthcode)) {
                System.out.println("成功");
                break;
            } else {
                System.out.println("验证码错误");
                continue;
            }
        }
        user userinfo = new user(username, password, null, null);
        boolean flag6 = checkuser(list, userinfo);
        if (flag6) {
            System.out.println("登录成功，欢迎进入");
                  Studentsystem ss=new Studentsystem();
                  ss.startSystem();
        } else {
            System.out.println("失败，用户名或密码有误");

        }


    }//登录
    public static boolean checkuser(ArrayList<user> list, user userinfo) {
        for (int i = 0; i < list.size(); i++) {
            if ((list.get(i).getUsername().equals(userinfo.getUsername())) && list.get(i).getPassword().equals(userinfo.getPassword())) {
                return true;
            }
        }
        return false;
    }////判断用户是否存在
    public static String Sysnumber() {
        ArrayList<Character> list = new ArrayList<>();//character--字符类型
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }

        Random ran = new Random();
        for (int i = 0; i < 4; i++) {
            //获取随机索引
            int index = ran.nextInt(list.size());
            sb.append(list.get(index));
        }
        int endnumber = ran.nextInt(9);
        sb.append(endnumber);
        char[] arr = sb.toString().toCharArray();//把字符串变成字符数组，在其中修改，再传建一个新的字符串
        int ranindex = ran.nextInt(arr.length - 1);
        char temp = arr[ranindex];
        arr[ranindex] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;
        return new String(arr);

    }//随机验证码
    public static void forgetpassword(ArrayList<user> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = contains(list, username);
        if (flag) {
            System.out.println("用户名不存在，请先注册");
            return;
        }
        user use = new user();
        while (true) {
            boolean flager = checkpassword(list, username);
            if (flager) {
                System.out.println("成功，请修改密码");
                break;
            } else {
                System.out.println("不一致，请重新输入 ");
                continue;
            }
        }
        String newpassword=sc.next();
        use.setPassword(newpassword);
        System.out.println("成功");
        return;
    }//忘记密码
    public static boolean checkpassword(ArrayList<user> list, String username) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入身份证号码");
        String personid = sc.next();
        System.out.println("请输入验证码");
        String phonenum = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (personid.equals(list.get(i).getPersonid()) && phonenum.equals(list.get(i).getPhonenumber())) {
                return true;
            }
        }
        return false;
    }
}










